package com.zhoujing.springsecurity.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhoujing.springsecurity.dto.ResponseResult;
import com.zhoujing.springsecurity.pojo.LoginUser;
import com.zhoujing.springsecurity.pojo.SysUser;
import com.zhoujing.springsecurity.service.SysUserService;
import com.zhoujing.springsecurity.mapper.SysUserMapper;
import com.zhoujing.springsecurity.utils.JwtUtil;
import com.zhoujing.springsecurity.utils.RedisCache;
import io.jsonwebtoken.Jwt;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* @author FanGccU
* @description 针对表【sys_user(用户表)】的数据库操作Service实现
* @createDate 2022-03-07 14:40:05
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private RedisCache redisCache;

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @Override
    public ResponseResult login(SysUser user) {
        //AuthenticationManager authenticationManager 进行用户认证
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUserName(),user.getPassword());
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        //如果认证通过了，使用userid生成一个jwt jwt存入 ResponseResult返回
        if(Objects.isNull(authenticate)){
            throw new RuntimeException("用户登录失败");
        }

        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        String id = loginUser.getSysUser().getId().toString();
        String jwt = JwtUtil.createJWT(id);
        Map<String,String> map = new HashMap<>();
        map.put("token",jwt);
        //把完整的用户信息存入redis userid作为key
        redisCache.setCacheObject("login:"+id,loginUser);
        return new ResponseResult(0,"登录成功",map);

    }

    /**
     * 退出登录
     */
    @Override
    public ResponseResult logout() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Long id = loginUser.getSysUser().getId();
        redisCache.deleteObject("login:"+id);
        return new ResponseResult(0,"退出成功");
    }
}




